package UILayer;
import ModelLayer.*;
import CtrlLayer.*;
import java.util.*;

public class MenuUI
{

    ProductsUI productsUI;
    CustomersUI customersUI;
    ContractorsUI contractorsUI;
    SalesUI salesUI;
    LeasesUI leasesUI;
    OrdersUI ordersUI;
    EmployeesUI employeesUI;
    StatisticsUI statisticsUI;
    SessionController session;
    ManagersUI managersUI;
    DeliveryUI deliveryUI;
    NoteUI noteUI;
    private int sessionId;

    public MenuUI()
    {
        productsUI = new ProductsUI();
        customersUI = new CustomersUI();
        contractorsUI = new ContractorsUI();
        salesUI = new SalesUI();
        leasesUI = new LeasesUI();
        ordersUI = new OrdersUI();
        employeesUI = new EmployeesUI();
        statisticsUI = new StatisticsUI();
        session = new SessionController();
        this.sessionId = session.createSession();
        managersUI = new ManagersUI();
        deliveryUI = new DeliveryUI();
        noteUI = new NoteUI();
        mainMenuStart();
    }

    public void mainMenuStart()
    {

        boolean exit=false;
        while (!exit) 
        {
            if(SessionContainer.getInstance().findSessionById(sessionId).isLoggedIn() ){
                int choice = writeMainMenu();
                switch(choice){
                    case 1:
                    productsUI.productsMenuStart(this.sessionId);
                    break;
                    case 2:
                    customersUI.customerMenuStart(this.sessionId);
                    break;
                    case 3:
                    contractorsUI.contractorsMenuStart(this.sessionId);
                    break;
                    case 4:
                    salesUI.salesMenuStart(this.sessionId);
                    break;
                    case 5:
                    leasesUI.leasesMenuStart(this.sessionId);
                    break;
                    case 6:
                    ordersUI.ordersMenuStart(this.sessionId);
                    break;
                    case 7:
                    deliveryUI.deliveryMenuStart(this.sessionId);
                    break;
                    case 8:
                    noteUI.noteMenuStart();
                    break;
                    case 9:
                    if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2 ){
                        employeesUI.employeesMenuStart();
                    }
                    break;
                    case 10:
                    if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2 ){
                        statisticsUI.statisticsMenuStart();
                    }
                    break;
                    case 11:
                    if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2 ){
                        managersUI.managersMenuStart();
                    }
                    break;

                    case 12:
                    SessionContainer.getInstance().findSessionById(sessionId).logOut();
                    break;
                    case 0:
                    writeEnd();
                    exit = true;
                    break;
                }
            }else{
                if(!writeLoginMenu()){
                    writeEnd();
                    session.findSession(this.sessionId).logOut();
                    exit = true;
                }
            }
        }
    }

    private boolean writeLoginMenu()
    {       
        int command =0;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Login ***");
        System.out.println(" Enter your id:");
        int id = Integer.parseInt(keyboard.nextLine());
        System.out.println(" Enter your password:");
        String password = keyboard.nextLine();
        boolean login = SessionContainer.getInstance().findSessionById(sessionId).login(id, password);
        if(!login){
            System.out.println("Wrong id or password");

            System.out.println("Press (1) to close or other key to try again");
            command = Integer.parseInt(keyboard.nextLine());
        }
        if(command == 1){
            return false;
        }else{
            return true;
        }

    }

    private int writeMainMenu(){       
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** MainMenu ***");
        System.out.println(" (1) Products");
        System.out.println(" (2) Customers");
        System.out.println(" (3) Contractors");
        System.out.println(" (4) Sales ");
        System.out.println(" (5) Leases");
        System.out.println(" (6) Orders");
        System.out.println(" (7) Delivery");
        System.out.println(" (8) Notes");
        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2 ){
            System.out.println(" (9) Employees");
            System.out.println(" (10) Statistics");
            System.out.println(" (11) Managers");
        }
        System.out.println(" (12) Logout");
        System.out.println(" (0) Close");
        System.out.print("\n Make your choice: ");
        int choice = keyboard.nextInt();
        return choice;
    }

    private void pause(){
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }

    private void writeEnd(){
        System.out.println(" The System is shuting down ");
    }
}
